<template>
    <view>
        <!-- 搜索框区域 -->
        <view class="search-view">
            <my-search @click="gotoSearch"></my-search>
        </view>
        <!-- 搜索框区域 end-->
        <!-- 轮播图区域 -->
        <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="true">
            <swiper-item v-for="item in swiperList" :key="item.goods_id">
                <navigator class="swiper-item" :url="`/subpck/goods_detail/goods_detail?goods_id=${item.goods_id}`">
                    <image :src="item.image_src"></image>
                </navigator>
            </swiper-item>
        </swiper>
        <!-- 轮播图区域 end-->
        <!-- 导航区域 -->
        <view class="nav-list">
            <view class="nav-item" v-for="(item,i) in navList" :key="i" @click="navClickHandle(item)">
                <image :src="item.image_src" class="nav-img"></image>
            </view>
        </view>
        <!-- 导航区域 end-->
        <!-- 楼层区域 -->
        <!-- 楼层容器 -->
        <view class="floor-list">
            <!-- 每一层楼层item项 -->
            <view class="floor-item" v-for="(item,i) in floorList" :key="i">
                <!-- 楼层标题 -->
                <image :src="item.floor_title.image_src" class="floor-title"></image>
                <!-- 楼层图片列表 -->
                <view class="floor-body-imgs">
                    <!-- 左侧大图片盒子 -->
                    <view class="left-img-box" @click="goGoodsList(item.product_list[0])">
                        <image :src="item.product_list[0].image_src"
                            :style="{width:item.product_list[0].image_width+'rpx'}" mode="widthFix">
                        </image>
                    </view>
                    <!-- 右侧四张小图片盒子 -->
                    <view class="right-img-box">
                        <view class="right-img-item" v-for="(item2 ,i2) in item.product_list" :key="i2" v-if="i2!==0"
                            @click="goGoodsList(item2)">
                            <image :src="item2.image_src" mode="widthFix" :style="{width:item2.image_width+'rpx'}">
                            </image>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <!-- 楼层区域 end-->
    </view>
</template>

<script>
    import badgeMixins from '@/mixins/badage-tabbar.js'
    export default {
        mixins: [badgeMixins],
        data() {
            return {
                // 轮播图数据
                swiperList: [],
                // 导航数据
                navList: [],
                // 楼层数据
                floorList: []
            };
        },
        onLoad() {
            // console.log(uni)
            this.getSwiperList()
            this.getNavList()
            this.getFloorList()
        },
        methods: {
            // 获取轮播图的数据
            async getSwiperList() {
                const {
                    data: res
                } = await uni.$http.get('/api/public/v1/home/swiperdata')
                // console.log(res);
                if (res.meta.status !== 200) return uni.$showMsg()
                this.swiperList = res.message
            },
            // 获取轮播图下导航数据
            async getNavList() {
                const {
                    data: res
                } = await uni.$http.get('/api/public/v1/home/catitems')
                // console.log(res);
                if (res.meta.status !== 200) return uni.$showMsg()
                this.navList = res.message
            },
            // 获取楼层数据
            async getFloorList() {
                const {
                    data: res
                } = await uni.$http.get('/api/public/v1/home/floordata')
                // console.log(res);
                if (res.meta.status !== 200) return uni.$showMsg()
                // 对返回的数据进行处理 添加个url属性
                // 遍历楼层数据的每一项
                res.message.forEach(item => {
                    // 遍历楼层数据每一项的product_list数组
                    item.product_list.forEach(prod => {
                        // product_list数组的每一项都添加一个url属性
                        // 把他自有的参数分割出来
                        prod.url = '/subpck/goods_list/goods_list?' + prod.navigator_url.split('?')[
                            1]

                    })
                })
                this.floorList = res.message
            },
            // 导航点击事件
            navClickHandle(item) {
                if (item.name === '分类') {
                    uni.switchTab({
                        url: '/pages/cate/cate'
                    })
                }
            },
            // 楼层点击事件 去往商品列表页面
            goGoodsList(item) {
                // console.log(item);
                uni.navigateTo({
                    url: item.url
                })
            },
            // 搜索框点击事件 去往搜索页
            gotoSearch() {
                uni.navigateTo({
                    url: '/subpck/search/search'
                })
            }

        }
    }
</script>

<style lang="scss">
    swiper {
        height: 330rpx;

        .swiper-item,
        image {
            width: 100%;
            height: 100%;
        }
    }

    .nav-list {
        display: flex;
        justify-content: space-around;
        margin: 15px 0;

        .nav-img {
            width: 128rpx;
            height: 140rpx;
        }
    }

    .floor-title {
        height: 60rpx;
        width: 100%;

    }

    .floor-body-imgs {
        display: flex;
        width: 100%;
        padding: 10rpx;

        .right-img-box {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
        }
    }

    .search-view {
        position: sticky;
        top: 0;
        left: 0;
        z-index: 999;
    }
</style>
